﻿using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;
using SdmxMl.GenericMetadata;
using SdmxMl.SanteSuisse;


namespace SilcQuest
{
    public class PdfQuestDetail
    {
        #region Internal Members

        private Rectangle pgSize;

        // Fonts used for table
        private Font fTableHeader;
        private Font fTableCell;
        private Font fTableCellBold;
        private Font fCodelistFullId;
        private RefVarHelper varHelper;
        private string lang;
        protected Document document;

        #endregion

        #region Constructor

        public PdfQuestDetail(Document doc, string lg, Rectangle r, RefVarHelper refVar)
        {
            document = doc;
            lang = lg;
            pgSize = r;
            varHelper = refVar;

            fTableHeader = new Font(Font.FontFamily.HELVETICA, 9, Font.NORMAL, BaseColor.WHITE);
            fTableCell = new Font(Font.FontFamily.HELVETICA, 9, Font.NORMAL);
            fTableCellBold = new Font(Font.FontFamily.HELVETICA, 9, Font.BOLD);
            fCodelistFullId = new Font(Font.FontFamily.HELVETICA, 7, Font.NORMAL);
        }

        #endregion

        #region Question Texts

        public PdfPTable PrepareSubQuest(CatiSilcQuestion question)
        {
            float[] colsWidth = { pgSize.Width * 0.99f, pgSize.Width * 0.01f };
            PdfPTable aTable = new PdfPTable(colsWidth);
            aTable.SetWidthPercentage(colsWidth, pgSize);
            aTable.DefaultCell.BorderWidth = 0;
            aTable.DefaultCell.BackgroundColor = BaseColor.WHITE;

            PdfPCell cell = new PdfPCell(new Phrase(question.SubQuest, fTableCellBold));
            cell.BorderWidth = 0;
            cell.BackgroundColor = BaseColor.WHITE;
            aTable.AddCell(cell);
            aTable.AddCell("");
            return aTable;
        }

        public PdfPTable PrepareQuestionTexts(CatiSilcQuestion question)
        {
            float[] colsWidth = { pgSize.Width * 0.20f, pgSize.Width * 0.80f };

            PdfPTable aTable = new PdfPTable(colsWidth.Length);
            aTable.SetWidthPercentage(colsWidth, pgSize);

            /*
            aTable.DefaultCell.BorderColorBottom = BaseColor.RED;
            aTable.DefaultCell.BorderWidth = 1;
            aTable.DefaultCell.BackgroundColor = BaseColor.WHITE;
            aTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
            aTable.DefaultCell.PaddingBottom = 6;
            aTable.DefaultCell.PaddingTop = 6;
            aTable.DefaultCell.PaddingLeft = 6;
            */
            

            FillOneText(aTable, "Filter:", question.Filter);
            FillOneText(aTable, "Plausi:", question.Plausi);
            FillOneText(aTable, "Intro:", question.IntroQuest);
            FillOneText(aTable, "Question:", question.Question);
            FillOneText(aTable, "Info Int:", question.InfoInterwiewer);

            return aTable;
        }

        private void FillOneText(PdfPTable aTable, string tag, string value)
        {
            PdfPCell cell = new PdfPCell(new Phrase(tag, fTableCell));

            cell.BorderColor = BaseColor.WHITE;
        //    cell.BorderColorBottom = BaseColor.LIGHT_GRAY;
         //   cell.BorderWidthBottom = 1;
         //   cell.BorderWidth = 0;
            cell.BackgroundColor = BaseColor.WHITE;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.PaddingBottom = 6;
            cell.PaddingTop = 6;
            cell.PaddingLeft = 6;
            aTable.AddCell(cell);


            cell = new PdfPCell(new Phrase(value, fTableCell));
            cell.BorderColor = BaseColor.WHITE;
           // cell.BorderColorBottom = BaseColor.LIGHT_GRAY;
           // cell.BorderWidth = 1;
            cell.BackgroundColor = BaseColor.WHITE;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.PaddingBottom = 6;
            cell.PaddingTop = 6;
            cell.PaddingLeft = 6;
            
            aTable.AddCell(cell);
        }

        #endregion

        #region Question Header

        public PdfPTable PrepareQuestionHeader(CatiSilcQuestion question, bool isTechnical)
        {
            float[] colsWidth = { pgSize.Width * 0.20f, pgSize.Width * 0.60f, pgSize.Width * 0.15f, pgSize.Width * 0.05f };

            PdfPTable aTable = new PdfPTable(colsWidth.Length);
            aTable.SetWidthPercentage(colsWidth, pgSize);

            aTable.DefaultCell.Border = Rectangle.NO_BORDER;
            aTable.DefaultCell.Padding = 3;
            aTable.DefaultCell.BorderWidth = 1;
            aTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;


            //Creates Variable row
            PdfPCell cell = new PdfPCell(new Phrase(question.VarName, fTableCell));
            cell.Border = Rectangle.ALIGN_LEFT;
            cell.BorderWidth = 2;
            cell.BackgroundColor = BaseColor.GRAY;
            aTable.AddCell(cell);

            cell = new PdfPCell(new Phrase(question.VarLabel, fTableCell));
            cell.Border = Rectangle.ALIGN_LEFT;
            cell.BorderWidth = 1;
            cell.BackgroundColor = BaseColor.LIGHT_GRAY;
            aTable.AddCell(cell);

            cell = new PdfPCell(new Phrase(isTechnical ? "Technical Question" : "Original Question", fTableCell));
            cell.Border = Rectangle.ALIGN_LEFT;
            cell.BorderWidth = 2;
            cell.BackgroundColor = BaseColor.LIGHT_GRAY;
            aTable.AddCell(cell);

            cell = new PdfPCell(new Phrase(question.WOrder, fTableCell));
            cell.Border = Rectangle.ALIGN_LEFT;
            cell.BorderWidth = 2;
            cell.BackgroundColor = BaseColor.GRAY;
            aTable.AddCell(cell);

            return aTable;
        }
        #endregion

        #region Prepare Code & Quality


        public PdfPTable PrepareCodeAndQuality(CatiSilcQuestion question)
        {
            float[] colsWidth = { pgSize.Width * 0.60f, pgSize.Width * 0.30f };
            PdfPTable aTable = new PdfPTable(colsWidth);
            aTable.SetWidthPercentage(colsWidth, pgSize);
            aTable.DefaultCell.BorderWidth = 0;
            aTable.DefaultCell.BackgroundColor = BaseColor.WHITE;

            PdfPCell cell = new PdfPCell(new Phrase("Values", fTableCellBold));
            cell.BorderWidth = 0;
            cell.BackgroundColor = BaseColor.WHITE;
            aTable.AddCell(cell);

            cell = new PdfPCell(new Phrase("Quality", fTableCellBold));
            cell.BorderWidth = 0;
            cell.BackgroundColor = BaseColor.WHITE;
            aTable.AddCell(cell);

            cell = new PdfPCell(new Phrase(question.VarCodes, fTableCell));
            cell.BorderWidth = 0;
            cell.BackgroundColor = BaseColor.WHITE;
            aTable.AddCell(cell);

            cell = new PdfPCell(new Phrase(question.QualityCodes, fTableCell));
            cell.BorderWidth = 0;
            cell.BackgroundColor = BaseColor.WHITE;
            aTable.AddCell(cell);


            return aTable;
        }

        #endregion
    }
}
